import GlobalData from "../../GlobalData";
import { ListenID } from "../../ListenID";
import { AlertManager } from "../../alert/AlertManager";
import { Common_UIPath } from "../../common/Common_Define";
import { Notifier } from "../../framework/Notifier";
import { UIManager } from "../../framework/UIManager";
import { Util } from "../../utils/Util";

const { ccclass, property } = cc._decorator;

@ccclass
export default class RuzhuItem extends cc.Component {

    @property(cc.Label)
    nick_name: cc.Label = null;

    @property(cc.Label)
    scoreText: cc.Label = null;

    @property(cc.Label)
    rankText: cc.Label = null;

    @property(cc.Sprite)
    avatar: cc.Sprite = null;

    // onLoad () {}

    private sf: cc.SpriteFrame;
    private ruzhuData: any;

    protected start(): void {
        this.sf = this.avatar.spriteFrame;
    }

    registerTouch() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchstart, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchend, this);
    }

    public touchstart() {

    }

    public touchend() {

    }

    setData(data) {
        this.ruzhuData = data;
        this.node.active = true;

        if (data.avatar) {
            Util.loadRemoteSf(data.avatar, this.avatar);
        }

        if (String(data.user_nickname).length > 10) {
            this.nick_name.string = String(data.user_nickname).substring(0, 10) + "...";
        } else {
            this.nick_name.string = data.user_nickname || "匿名玩家";
        }

        this.scoreText.string = Util.convertTimestampToDateTime(Number(data.create_time)) || `--`;

    }

    onClickTiaozhan() {
        if (GlobalData.inst.uid == this.ruzhuData.uid) {
            AlertManager.showNormalTips("不能挑战自己");
            return;
        }
        if(GlobalData.inst.findMe){
            AlertManager.showNormalTips("你已经在岛上");
            return;
        }
        
        this.onClose();
        GlobalData.inst.tiaozhandata.default_gunId = this.ruzhuData.default_gunId || 105;
        GlobalData.inst.tiaozhandata.battle = Number(this.ruzhuData.battle) || 100;
        GlobalData.inst.tiaozhandata.uid = this.ruzhuData.uid || "";
        GlobalData.inst.tiaozhandata.baseAllHp = Number(this.ruzhuData.baseAllHp) || 300;
        GlobalData.inst.tiaozhandata.island_id = this.ruzhuData.island_id;
        GlobalData.inst.tiaozhandata.type = "ruzhu";
        console.log("进入挑战房间", GlobalData.inst.tiaozhandata);
        cc.director.loadScene("GameTiaozhan");
    }

    public onClose(): void {
        UIManager.Close(Common_UIPath.RuzhuView);
        UIManager.Close(Common_UIPath.MenuUI);
        UIManager.Close(Common_UIPath.CurrencyUI);
        this.unscheduleAllCallbacks();
        Notifier.send(ListenID.Menu_Open, false);
        Notifier.send(ListenID.On_Close_Menu);
    }

    // update (dt) {}
}
